
#include <iostream>
using namespace std;
class A
{
public:
	A(int a = 0)
		: _a(a)
	{
		cout << "A():" << this << endl;
	}
	~A()
	{
		cout << "~A()" << this << endl;
	}
private:
	int _a;
};


int main()
{
	//A* p1 = new A;   //和下面等价
	A* p1 = (A*)operator new(sizeof(A));
	//p1->A(); //不支持这样显示调用构造
	new(p1)A; //对已有空间显示调用构造

	// new []
	A* p2 = (A*)operator new(sizeof(A)*10);
	//new(p2)A[10]; //对已有空间显示调用构造
	//new(p2)A[10]{ 1,2,3,4 }; //从语法上来说是可以的
	for (int i = 0; i < 10; ++i) new(p2 + i)A(i); //用for循环来初始化每个对象
	
	//delete []
	for (int i = 0; i < 10; i++) (p2 + i)->~A(); //析构每个对象
	operator delete[](p2);

	// delete p1 //和下面等价
	p1->~A();
	operator delete(p1);

	return 0;
}
